// set function parseTime,formatTime to filter
export {parseTime, formatTime} from '@/utils'

function pluralize(time, label) {
  if (time === 1) {
    return time + label
  }
  return time + label + 's'
}

export function timeAgo(time) {
  const between = Date.now() / 1000 - Number(time)
  if (between < 3600) {
    return pluralize(~~(between / 60), ' minute')
  } else if (between < 86400) {
    return pluralize(~~(between / 3600), ' hour')
  } else {
    return pluralize(~~(between / 86400), ' day')
  }
}

/* 数字 格式化*/
export function numberFormatter(num, digits) {
  const si = [
    {value: 1E18, symbol: 'E'},
    {value: 1E15, symbol: 'P'},
    {value: 1E12, symbol: 'T'},
    {value: 1E9, symbol: 'G'},
    {value: 1E6, symbol: 'M'},
    {value: 1E3, symbol: 'k'}
  ]
  for (let i = 0; i < si.length; i++) {
    if (num >= si[i].value) {
      return (num / si[i].value + 0.1).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol
    }
  }
  return num.toString()
}

export function toThousandFilter(num) {
  return (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
}


export function formatGender(value) {
  return value === 1 ? '男' : '女';
}


export const orderTypes = [
  {label: "美食", value: 1},
  {label: "早餐", value: 2},
  {label: "跑腿代购", value: 3},
  {label: "汉堡披萨", value: 4},
  {label: "甜品饮品", value: 5},
  {label: "速食简餐", value: 6},
  {label: "地方小吃", value: 7},
  {label: "米粉面馆", value: 8},
  {label: "包子粥铺", value: 9},
  {label: "炸鸡炸串", value: 10}
]

export function getOrderType(value) {
  let ret = orderTypes.filter(x => x.value == value);
  return ret[0].label;
}
